package com.warrior.superdata.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.Objects;
import java.util.Properties;

@Component
@Slf4j
public class PropertiesUtil {


    public Properties properties;

    /**
     * 读取 application-dataType.yaml 配置文件信息
     */
    public PropertiesUtil() {
        // application-datatype.yaml配置文件信息
        ClassPathResource resource = new ClassPathResource("/application-datatype.yaml");
        try {
            properties = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            log.error("获取application-datatype配置文件异常 errMsg:{}", e.getMessage());

        }
    }

    public String getJavaTypeByFieldType(String fieldType) {
        String javaType = properties.getProperty(fieldType);
        if ((javaType == null || Objects.equals(javaType, "")) && fieldType.indexOf("char") > -1) {
            javaType = properties.getProperty("varchar");
        }
        if ((javaType == null || Objects.equals(javaType, "")) && fieldType.indexOf("decimal") > -1) {
            javaType = properties.getProperty("decimal");
        }
        return javaType;
    }

}
